<?php

use yii\helpers\Html;
//use yii\grid\GridView;
use kartik\grid\GridView;
use common\models\business\BusinessInspection;
use yii\helpers\ArrayHelper;
$healthy = $pigpens = $feeds = [];
foreach (BusinessInspection::$healthys as $key => $row){
    $healthy[$row] = $row;
}
foreach (BusinessInspection::$pigpens as $key => $row){
    $pigpens[$row] = $row;
}
foreach (BusinessInspection::$feeds as $key => $row){
    $feeds[$row] = $row;
}

/* @var $this yii\web\View */
/* @var $searchModel backend\modules\business\models\BusinessInspectionSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '巡检管理';
$this->params['breadcrumbs'][] = $this->title;
?>


<section class="wrapper site-min-height">
    <!-- page start-->
    <section class="panel">
        <header class="panel-heading">
              <?= Html::a('返回','javascript:history.back(-1)'); ?>  <?= Html::encode($this->title) ?>
        </header>
        <div class="panel-body">
            <div class="adv-table editable-table ">
 


<div class="business-inspection-index">

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <?php
        echo GridView::widget([
            'dataProvider'=>$dataProvider,
            'filterModel'=>$searchModel,
            'showPageSummary'=>true,
            'pjax'=>false,
            'striped'=>true,
            'hover'=>true,
            'panel'=>['type'=>'primary', 'heading'=>'巡检管理'],
            'containerOptions'=>['style'=>'overflow: auto'], // only set when $responsive = false
            'headerRowOptions'=>['class'=>'kartik-sheet-style'],
            'filterRowOptions'=>['class'=>'kartik-sheet-style'],
            'columns'=>[

                [
                    'label' => '仔猪编号',
                    'attribute'=>'pigno',
                    'format' => 'raw',
                    'value' => function($model){
                        return $model->pig?Html::a($model->pig->no,\yii\helpers\Url::to(['/business/pig/view','id'=> $model->pig->id])):'';
                    },
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'100px',
                ],
                [
                    'attribute'=>'business_personid',
                    'format' => 'raw',
                    'value' => function($model){
                        return $model->person?Html::a($model->person->name,\yii\helpers\Url::to(['/business/person/view','id'=> $model->person->id])):'';
                    },
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'100px',
                    'filterType'=>GridView::FILTER_SELECT2,
                    'filter'=>ArrayHelper::map(\common\models\business\BusinessPerson::find()->asArray()->all(),'id','name'),
                    'filterWidgetOptions'=>[
                        'pluginOptions'=>['allowClear'=>true],
                    ],
                    'filterInputOptions'=>['placeholder'=>'业务员'],
                ],
                [
                    'attribute'=>'healthy',
                    'width'=>'100px',
                    'filterType'=>GridView::FILTER_SELECT2,
                    'filter'=> $healthy,
                    'filterWidgetOptions'=>[
                        'pluginOptions'=>['allowClear'=>true],
                    ],
                    'filterInputOptions'=>['placeholder'=>'仔猪健康状况'],
//                    'group'=>true,  // enable grouping
                ],
                [
                    'attribute'=>'pigpen',
                    'width'=>'150px',
                    'filterType'=>GridView::FILTER_SELECT2,
                    'filter'=> $pigpens,
                    'filterWidgetOptions'=>[
                        'pluginOptions'=>['allowClear'=>true],
                    ],
                    'filterInputOptions'=>['placeholder'=>'选择猪舍状况']
                ],
                [
                    'attribute'=>'feed',
                    'width'=>'150px',
                    'filterType'=>GridView::FILTER_SELECT2,
                    'filter'=> $feeds,
                    'filterWidgetOptions'=>[
                        'pluginOptions'=>['allowClear'=>true],
                    ],
                    'filterInputOptions'=>['placeholder'=>'食料状况'],
                    'width'=>'150px',
                ],
                [
                    'filterType'=>GridView::FILTER_DATE,
                    'class' => '\kartik\grid\DataColumn',
                    'filterWidgetOptions'=>[
                        'pluginOptions'=>['format' => 'yyyy-mm-dd'],
                    ],
                    'attribute' => 'createtime',
                    'value'=>
                        function($model){
                            return  date('Y-m-d H:i:s',$model->createtime);   //主要通过此种方式实现
                        },
                    'width'=>'150px',
                ],
                [
                    'attribute'=>'height',
                    'width'=>'80px',
                    'hAlign'=>'right',
                    'pageSummary'=>true
                ],
                [
                    'attribute'=>'status',
                    'value' => function($model){
                        if($model->status==BusinessInspection::STATUS_PASSED) {
                            return Html::label(BusinessInspection::$statues[$model->status], '', ['class' => 'btn btn-xs btn-success']);
                        }else if($model->status==BusinessInspection::STATUS_UNPASSED) {
                            return Html::label(BusinessInspection::$statues[$model->status], '', ['class' => 'btn btn-xs btn-danger']);
                        }
                        return Html::label(BusinessInspection::$statues[$model->status], '', ['class' => 'btn btn-xs btn-default']);
                    },
                    'format' => 'raw',
                    'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                    'width'=>'100px',
                    'filterType'=>GridView::FILTER_SELECT2,
                    'filter'=>BusinessInspection::$statues,
                    'filterWidgetOptions'=>[
                        'pluginOptions'=>['allowClear'=>true],
                    ],
                    'filterInputOptions'=>['placeholder'=>'是否通过审核'],
                ],
                [
                    'class' => '\kartik\grid\ActionColumn',
                    'deleteOptions' => ['label' => '<i class="glyphicon glyphicon-remove"></i>']
                ],
                [
                    'class' => '\kartik\grid\CheckboxColumn'
                ],
            ],
            'toolbar'=> [
                [
                    'content' => Html::button('<i class="glyphicon glyphicon-delete">批量批量审核</i>', ['type'=>'button', 'title'=>'批量设置出栏时间', 'class'=>'btn btn-success', 'id'=>'setpass'])
                ],
                [
                    'content' => Html::button('<i class="glyphicon glyphicon-delete">批量删除</i>', ['type'=>'button', 'title'=>'批量设置出栏时间', 'class'=>'btn btn-success', 'id'=>'mutidelete'])
                ],
                '{export}',
                '{toggleData}',
            ],
        ]);
    ?>
</div>
</div>
</div>
</section>
</section>

<script>
    <?php $this->beginBlock('js_end') ?>

    $('#setpass').click(function () {
        var keys = jqchk();
        if(confirm('确定要审核通过选中的巡检记录吗')){
            if(keys.length <= 0){
                alert('请选中对应的巡检记录');
                return false;
            }
            $.post('/business/inspection/setpass',{'ids':keys},function(res){
                console.log(res);
                alert(res.msg);
                if(res.status == 0)
                    window.location.href='/business/inspection/index';
            },'json');
        }
    });
    $('#mutidelete').click(function () {
        var keys = jqchk();
        if(confirm('确定要删除选中的巡检记录吗')){
            if(keys.length <= 0){
                alert('请选中对应的巡检记录');
                return false;
            }
            $.post('/business/inspection/muti-delete',{'ids':keys},function(res){
                console.log(res);
                alert(res.msg);
                if(res.status == 0)
                    window.location.href='/business/inspection/index';
            },'json');
        }
    });

    function jqchk() { //jquery获取复选框值=
        var chk_value = [];
        $('input[name="selection[]"]:checked').each(function () {
            chk_value.push($(this).val());
        });
        return chk_value;
    }


    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>
</script>







